package com.hrm.service.impl;

import com.github.pagehelper.PageHelper;
import com.hrm.mapper.DeptMapper;
import com.hrm.pojo.Dept;
import com.hrm.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;


    @Override
    public List<Dept> findAllLikeName(Integer pageNum, Integer pageSize, String dept)throws Exception {
        PageHelper.startPage(pageNum,pageSize);
        return deptMapper.selectDeptListLikeName(dept);
    }

    @Override
    public List<Dept> findAll() throws Exception {
        return deptMapper.selectDeptList();
    }

    @Override
    public boolean addDept(Dept dept) {
        if(deptMapper.insertSelective(dept)>0)
            return true;
        return false;
    }

    @Override
    public boolean deleteDept(ArrayList<Integer> deptIdList) throws Exception {
        for(Integer id:deptIdList){
            if(deptMapper.deleteByPrimaryKey(id)<=0){
                throw new Exception("删除失败");
            }

        }

        return true;
    }

    @Override
    public boolean updateDept(Dept dept) {
        if(deptMapper.updateByPrimaryKeySelective(dept)>0)
            return true;
        return false;
    }

    @Override
    public Dept findById(Integer id) {
        return deptMapper.selectByPrimaryKey(id);
    }
}
